package com.linzelin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    //这里是获取连接池对象

    @RequestMapping("/userList")
    public List<Map<String,Object>> userList(){
        String sql = "select * from user";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        System.out.println(maps);
        return maps;
    }

    @RequestMapping("/addUser")
    public String addUser(){
        String sql = "insert into user values(null,'狗','123456',1)";
        int update = jdbcTemplate.update(sql);
        return update+"";
    }

    @RequestMapping("/updateUser/{id}")
    public int updateUser(@PathVariable("id") int id){
        String sql = "update user set username=?,password=? where id=?";
        return jdbcTemplate.update(sql,"xiexie","1234aaa",id);
    }

    @RequestMapping("deleteUser/{id}")
    public int deleteUser(@PathVariable("id")   int id){
        String sql = "delete from user where id=?";
        return jdbcTemplate.update(sql,id);
    }

}
